package org.csu.mypetstore.persistence.impl;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.persistence.CartDAO;
import org.csu.mypetstore.persistence.DBUtil;
import org.csu.mypetstore.persistence.ItemDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class CartDAOImpl implements CartDAO {

    private static final String getCartByUsername = "SELECT * FROM cart WHERE userName = ?";


    private static final String insertCartByUsername = "insert into cart(userName,itemId,quantity) values (?,?,?) ";
    private static final String updateCartQuantity = "update cart set quantity = ? where userName = ? and itemId = ?";
    private static final String deleteCartItemByUsername = "delete from cart where userName = ? and itemId = ?";
    private static final String delteALL = "delete from cart where userName = ?";

    ItemDAO itemDAO = new ItemDAOImpl();

    @Override
    public Cart getCartByUsername(String username) {

        Cart cart = new Cart();
        try {
            Connection connection = DBUtil.getConnetion();
            PreparedStatement preparedStatement = connection.prepareStatement(getCartByUsername);

            preparedStatement.setString(1,username);

            ResultSet resultSet = preparedStatement.executeQuery();

            int i = 0;
            while (resultSet.next()){
                Item item = itemDAO.getItem(resultSet.getString(2));
                int x= Integer.parseInt(resultSet.getString(3));
                item.setQuantity(x);

                cart.addItem(item,true);
                cart.getCartItemList().get(i).setQuantity(x);
                i++;
                }

            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return cart;
    }

    @Override
    public void insertCartByUsername(String username,String itemId,int quantity) {
        try {
            Connection connection = DBUtil.getConnetion();
            PreparedStatement preparedStatement = connection.prepareStatement(insertCartByUsername);

            preparedStatement.setString(1,username);
            preparedStatement.setInt(3,quantity);

            preparedStatement.setString(2,itemId);

            preparedStatement.execute();


            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void updateCartQuantity(String username, String itemId, int quantity) {
        try {
            Connection connection = DBUtil.getConnetion();
            PreparedStatement preparedStatement = connection.prepareStatement(updateCartQuantity);

            preparedStatement.setInt(1,quantity);
            preparedStatement.setString(2,username);
            preparedStatement.setString(3,itemId);

            preparedStatement.execute();


            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteCarItemtByUsername(String username, String itemId) {
        try {
            Connection connection = DBUtil.getConnetion();
            PreparedStatement preparedStatement = connection.prepareStatement(deleteCartItemByUsername);


            preparedStatement.setString(1,username);
            preparedStatement.setString(2,itemId);

            preparedStatement.execute();


            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void delteALL(String username) {
        try {
            Connection connection = DBUtil.getConnetion();
            PreparedStatement preparedStatement = connection.prepareStatement(delteALL);


            preparedStatement.setString(1,username);


            preparedStatement.execute();


            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


}
